package apollo.generator.modules.controller.config;

import apollo.generator.common.annotation.SysLog;
import apollo.generator.common.base.BaseController;
import apollo.generator.common.base.R;
import apollo.generator.common.base.RR;
import apollo.generator.common.exception.CustomException;
import apollo.generator.common.utils.PageUtils;
import apollo.generator.modules.entity.PreferenceEntity;
import apollo.generator.modules.service.PreferenceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * <Description>
 * 偏好配置
 * @author Jun Lee
 * @email tommy2015yoyo@163.com
 * @createDate 2020-07-02 10:21:00
 */
@RestController
@RequestMapping("config/preference")
public class PreferenceController extends BaseController {
    @Autowired
    private PreferenceService preferenceService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("config:preference:list")
    public R list(@RequestParam Map<String, Object> params) throws CustomException {
        PageUtils page = preferenceService.queryPage(params);
        return RR.success(page);
    }

    /**
     * 启用/禁用
     */
    @SysLog("启用/禁用偏好配置")
    @RequestMapping("/enable/{id}/{enable}")
    @RequiresPermissions("config:preference:enable")
    public R enable(@PathVariable("id") Long id,@PathVariable("enable") Integer enable){
       preferenceService.enable(id,enable,getUserId());
        return RR.success();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("config:preference:info")
    public R info(@PathVariable("id") Long id){
		PreferenceEntity preference = preferenceService.getById(id);
        return RR.success(preference);
    }

    /**
     * 保存
     */
    @SysLog("保存偏好配置")
    @RequestMapping("/save")
    @RequiresPermissions("config:preference:save")
    public R save(@RequestBody PreferenceEntity preference){
        preference.setCreateUserId(getUserId());
		preferenceService.save(preference);
        return RR.success();
    }

    /**
     * 修改
     */
    @SysLog("修改偏好配置")
    @RequestMapping("/update")
    @RequiresPermissions("config:preference:update")
    public R update(@RequestBody PreferenceEntity preference){
		preferenceService.updateById(preference);
        return RR.success();
    }

    /**
     * 删除
     */
    @SysLog("删除偏好配置")
    @RequestMapping("/delete")
    @RequiresPermissions("config:preference:delete")
    public R delete(@RequestBody Long[] ids){
		preferenceService.removeByIds(Arrays.asList(ids));
        return RR.success();
    }

}
